package com.freeLearning.boot.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freeLearning.boot.shared.websocket.model.ChatMessage;
import com.freeLearning.boot.system.mapper.MessageMapper;
import com.freeLearning.boot.system.mapper.UserMapper;
import com.freeLearning.boot.system.model.entity.Message;
import com.freeLearning.boot.system.model.vo.MessageVO;
import com.freeLearning.boot.system.service.MessageService;
import com.freeLearning.boot.system.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    private final SimpMessagingTemplate messagingTemplate;
    private final UserMapper userMapper;

    public boolean sendMessage(Message message) {
        // 接收人
        String receiver = userMapper.selectById(message.getAcceptId()).getUsername();
        MessageVO messageVO = BeanUtil.copyProperties(message, MessageVO.class);
        messageVO.setIsMessage(true);
        messagingTemplate.convertAndSendToUser(receiver, "/queue/message", messageVO);
        return save(message);
    }
}
